//package org.jeecg.modules.stocktake.controller;
//
//import java.util.*;
//import java.util.stream.Collectors;
//import java.io.IOException;
//import java.io.UnsupportedEncodingException;
//import java.net.URLDecoder;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//
//import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
//import com.baomidou.mybatisplus.core.toolkit.Wrappers;
//import com.baomidou.mybatisplus.extension.api.R;
//import io.swagger.annotations.ApiModelProperty;
//import org.apache.poi.ss.formula.functions.T;
//import org.apache.shiro.SecurityUtils;
//import org.jeecg.common.api.vo.Result;
//import org.jeecg.common.aspect.annotation.Dict;
//import org.jeecg.common.system.query.QueryGenerator;
//import org.jeecg.common.system.vo.LoginUser;
//import org.jeecg.common.util.oConvertUtils;
//import org.jeecg.modules.basicShelf.entity.BasicShelf;
//import org.jeecg.modules.basicShelf.service.IBasicShelfService;
//import org.jeecg.modules.basicStationInfo.entity.BasicStationInfo;
//import org.jeecg.modules.basicStationInfo.service.IBasicStationInfoService;
//import org.jeecg.modules.reportLabel.entity.ReportLabel;
//import org.jeecg.modules.reportLabel.service.IReportLabelService;
//import org.jeecg.modules.stocktake.entity.Stocktake;
//import org.jeecg.modules.stocktake.service.IStocktakeService;
//
//import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
//import com.baomidou.mybatisplus.core.metadata.IPage;
//import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
//import lombok.extern.slf4j.Slf4j;
//
//import org.jeecgframework.poi.excel.ExcelImportUtil;
//import org.jeecgframework.poi.excel.annotation.Excel;
//import org.jeecgframework.poi.excel.def.NormalExcelConstants;
//import org.jeecgframework.poi.excel.entity.ExportParams;
//import org.jeecgframework.poi.excel.entity.ImportParams;
//import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
//import org.jeecg.common.system.base.controller.JeecgController;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.transaction.annotation.Transactional;
//import org.springframework.web.bind.annotation.*;
//import org.springframework.web.multipart.MultipartFile;
//import org.springframework.web.multipart.MultipartHttpServletRequest;
//import org.springframework.web.servlet.ModelAndView;
//import com.alibaba.fastjson.JSON;
//import io.swagger.annotations.Api;
//import io.swagger.annotations.ApiOperation;
//import org.jeecg.common.aspect.annotation.AutoLog;
//
///**
// * @Description: 盘点
// * @Author: jeecg-boot
// * @Date: 2024-06-28
// * @Version: V1.0
// */
//@Api(tags = "盘点")
//@RestController
//@RequestMapping("/stocktake/stocktake")
//@Slf4j
//public class StocktakeController extends JeecgController<Stocktake, IStocktakeService> {
//    @Autowired
//    private IStocktakeService stocktakeService;
//    @Autowired
//    private IReportLabelService reportLabelService;
//    @Autowired
//    private IBasicStationInfoService stationService;
//    @Autowired
//    private IBasicShelfService basicShelfService;
//
//
//
//
//
//
//    /**
//     *   差异原因
//     *
//     * @param stocktake
//     * @return
//     */
//    @AutoLog(value = "盘点-获取货架id")
//    @ApiOperation(value = "盘点-获取货架id", notes = "盘点-获取货架id")
//    @PostMapping(value = "/getShelf")
//    @Transactional
//    public Result<?> getShelf(@RequestBody Stocktake stocktake) {
//        BasicShelf one = basicShelfService.getOne(new LambdaQueryWrapper<BasicShelf>().eq(BasicShelf::getShelfCode, stocktake.getLocation()));
//        List<String> list = new ArrayList<>();
//        list.add(one.getId());
//        return Result.OK("成功！", list);
//    }
//    /**
//     *   差异原因
//     *
//     * @param stocktake
//     * @return
//     */
//    @AutoLog(value = "盘点-差异原因")
//    @ApiOperation(value = "盘点-差异原因", notes = "盘点-差异原因")
//    @PostMapping(value = "/sycn")
//    @Transactional
//    public Result<?> sycn(@RequestBody Stocktake stocktake) {
//        ReportLabel reportLabel = reportLabelService.getOne(new LambdaQueryWrapper<ReportLabel>().eq(ReportLabel::getSkuCode, stocktake.getSkuCode()).eq(ReportLabel::getArea, stocktake.getArea()).eq(ReportLabel::getShelfCode, stocktake.getLocation()));
//        reportLabel.setAvailableInventory(stocktake.getStocktakeNum());
//        reportLabelService.saveOrUpdate(reportLabel);
//        if (stocktake.getStatus() == 1) {
//            stocktake.setStatus(3);
//        }
//        if (stocktake.getStatus() == 2) {
//            stocktake.setStatus(4);
//        }
//        stocktake.setConfirmDate(new Date());
//        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
//        stocktake.setConfirmUser(user.getRealname());
//        stocktakeService.saveOrUpdate(stocktake);
//        return Result.OK("成功！", null);
//    }
//
//    /**
//     *   数量
//     *
//     * @param stocktake
//     * @return
//     */
//    @AutoLog(value = "盘点-数量")
//    @ApiOperation(value = "盘点-数量", notes = "盘点-数量")
//    @PostMapping(value = "/confirmStocktakingNumber")
//    public Result<?> confirmStocktakingNumber(@RequestBody Stocktake stocktake) {
//        if (stocktake.getSkuCode() == null || stocktake.getSkuCode().isEmpty()) {
//            return Result.error("请输入品番编码");
//        }
//        //if (stocktake.getSkuName() == null || stocktake.getSkuName().isEmpty()) {
//        //    return Result.error("请输入品番名称");
//        //}
//        if (stocktake.getArea() == null || stocktake.getArea().isEmpty()) {
//            return Result.error("请输入所属库区");
//        }
//        if (stocktake.getLocation() == null || stocktake.getLocation().isEmpty()) {
//            return Result.error("请输入货架");
//        }
//        stocktake.setDisparityNum(stocktake.getNumber() - stocktake.getStocktakeNum());
//        Integer disparityNum = stocktake.getDisparityNum();
//        if (disparityNum == 0) {
//            stocktake.setStatus(5);
//            stocktake.setRemarks("盘平");
//            stocktake.setConfirmDate(new Date());
//            LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
//            stocktake.setConfirmUser(user.getRealname());
//        } else if (disparityNum < 0) {
//            stocktake.setStatus(1);
//        } else {
//            stocktake.setStatus(2);
//        }
//        stocktakeService.saveOrUpdate(stocktake);
//        return Result.OK("添加成功！", null);
//    }
//
//    /**
//     *   生成盘点
//     *
//     * @param reportLabels
//     * @return
//     */
//    @AutoLog(value = "盘点-生成盘点")
//    @ApiOperation(value = "盘点-生成盘点", notes = "盘点-生成盘点")
//    @PostMapping(value = "/saveStocktaking")
//    public Result<?> saveStocktaking(@RequestBody List<ReportLabel> reportLabels) {
//        List<Stocktake> stocktakes = new ArrayList<>();
//        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
//        for (ReportLabel reportLabel : reportLabels) {
//            Stocktake stocktake = new Stocktake();
//            stocktake.setArea(reportLabel.getArea());
//            stocktake.setSkuCode(reportLabel.getSkuCode());
//            stocktake.setNumber(reportLabel.getAvailableInventory());
//            stocktake.setStatus(0);
//            stocktake.setStocktakeDate(new Date());
//            stocktake.setOperateUser(user.getRealname());
//            stocktake.setLocation(reportLabel.getShelfCode());
//            stocktakes.add(stocktake);
//        }
//        stocktakeService.saveOrUpdateBatch(stocktakes);
//        return Result.OK("添加成功！", null);
//    }
//
//
//    /**
//     * 分页列表查询
//     *
//     * @param stocktake
//     * @param pageNo
//     * @param pageSize
//     * @param req
//     * @return
//     */
//    @AutoLog(value = "盘点-分页列表查询")
//    @ApiOperation(value = "盘点-分页列表查询", notes = "盘点-分页列表查询")
//    @GetMapping(value = "/list")
//    public Result<?> queryPageList(Stocktake stocktake, @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo, @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, HttpServletRequest req) {
//        String skuCode = stocktake.getSkuCode();
//        String skuName = stocktake.getSkuName();
//        String area = stocktake.getArea();
//        String location = stocktake.getLocation();
//        Integer number = stocktake.getNumber();
//        Integer stocktakeNum = stocktake.getStocktakeNum();
//        Integer disparityNum = stocktake.getDisparityNum();
//        String remarks = stocktake.getRemarks();
//        String confirmUser = stocktake.getConfirmUser();
//        String operateUser = stocktake.getOperateUser();
//        stocktake.setSkuCode(null);
//        stocktake.setSkuName(null);
//        stocktake.setArea(null);
//        stocktake.setLocation(null);
//        stocktake.setNumber(null);
//        stocktake.setStocktakeNum(null);
//        stocktake.setDisparityNum(null);
//        stocktake.setRemarks(null);
//        stocktake.setConfirmUser(null);
//        stocktake.setOperateUser(null);
//        QueryWrapper<Stocktake> queryWrapper = QueryGenerator.initQueryWrapper(stocktake, req.getParameterMap());
//        if (skuCode != null && !skuCode.isEmpty()) {
//            queryWrapper.like("sku_code", skuCode);
//        }
//        if (skuName != null && !skuName.isEmpty()) {
//            queryWrapper.like("sku_name", skuName);
//        }
//        if (area != null && !area.isEmpty()) {
//            queryWrapper.like("area", area);
//        }
//        if (location != null && !location.isEmpty()) {
//            queryWrapper.like("location", location);
//        }
//        if (remarks != null && !remarks.isEmpty()) {
//            queryWrapper.like("remarks", remarks);
//        }
//        if (number != null) {
//            queryWrapper.like("number", number);
//        }
//        if (stocktakeNum != null) {
//            queryWrapper.like("stocktake_num", stocktakeNum);
//        }
//        if (disparityNum != null) {
//            queryWrapper.like("disparity_num", disparityNum);
//        }
//        if (confirmUser != null) {
//            queryWrapper.like("confirm_user", confirmUser);
//        }
//        if (operateUser != null) {
//            queryWrapper.like("operate_user", operateUser);
//        }
//        Page<Stocktake> page = new Page<Stocktake>(pageNo, pageSize);
//        IPage<Stocktake> pageList = stocktakeService.page(page, queryWrapper);
//        return Result.OK(pageList);
//    }
//
//    /**
//     *   添加
//     *
//     * @param stocktake
//     * @return
//     */
//    @AutoLog(value = "盘点-添加")
//    @ApiOperation(value = "盘点-添加", notes = "盘点-添加")
//    @PostMapping(value = "/add")
//    public Result<?> add(@RequestBody Stocktake stocktake) {
//        ReportLabel reportLabel = reportLabelService.getOne(new LambdaQueryWrapper<ReportLabel>().eq(ReportLabel::getSkuCode, stocktake.getSkuCode()).eq(ReportLabel::getArea, stocktake.getArea()).eq(ReportLabel::getShelfCode, stocktake.getLocation()));
//        if (reportLabel == null) {
//            return Result.error("添加失败,未找到该库存");
//        }
//        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
//        stocktake.setStatus(0);
//        stocktake.setNumber(reportLabel.getAvailableInventory());
//        stocktake.setStocktakeDate(new Date());
//        stocktake.setOperateUser(user.getRealname());
//        stocktakeService.save(stocktake);
//        return Result.OK("添加成功！");
//    }
//
//    /**
//     *  呼叫编辑
//     *
//     * @param stocktake
//     * @return
//     */
//    @AutoLog(value = "盘点-呼叫编辑")
//    @ApiOperation(value = "盘点-呼叫编辑", notes = "盘点-呼叫编辑")
//    @PostMapping (value = "/callShelfEdit")
//    public Result<?> callShelfEdit(@RequestBody Stocktake stocktake) {
//        String stationName = stocktake.getStationCode();
//        LambdaQueryWrapper<BasicStationInfo> stationLqw = Wrappers.lambdaQuery(BasicStationInfo.class);
//        stationLqw.eq(BasicStationInfo::getStationName, stationName);
//        BasicStationInfo station = stationService.getOne(stationLqw);
//        stocktake.setStationCode(station.getStationCode());
//        stocktakeService.updateById(stocktake);
//        return Result.OK("呼叫编辑成功!");
//    }
//
//    /**
//     *  编辑
//     *
//     * @param stocktake
//     * @return
//     */
//    @AutoLog(value = "盘点-编辑")
//    @ApiOperation(value = "盘点-编辑", notes = "盘点-编辑")
//    @PutMapping(value = "/edit")
//    public Result<?> edit(@RequestBody Stocktake stocktake) {
//        ReportLabel reportLabel = reportLabelService.getOne(new LambdaQueryWrapper<ReportLabel>().eq(ReportLabel::getSkuCode, stocktake.getSkuCode()).eq(ReportLabel::getArea, stocktake.getArea()).eq(ReportLabel::getShelfCode, stocktake.getLocation()));
//        if (reportLabel == null) {
//            return Result.error("编辑失败,未找到该库存");
//        }
//        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
//        stocktake.setNumber(reportLabel.getAvailableInventory());
//        stocktake.setStatus(0);
//        if (stocktake.getStocktakeDate() == null) {
//            stocktake.setStocktakeDate(new Date());
//        }
//        if (stocktake.getOperateUser() == null || stocktake.getOperateUser().isEmpty()) {
//            stocktake.setOperateUser(user.getRealname());
//        }
//        stocktakeService.updateById(stocktake);
//        return Result.OK("编辑成功!");
//    }
//
//    /**
//     *   通过id删除
//     *
//     * @param id
//     * @return
//     */
//    @AutoLog(value = "盘点-通过id删除")
//    @ApiOperation(value = "盘点-通过id删除", notes = "盘点-通过id删除")
//    @DeleteMapping(value = "/delete")
//    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
//        stocktakeService.removeById(id);
//        return Result.OK("删除成功!");
//    }
//
//    /**
//     *  批量删除
//     *
//     * @param ids
//     * @return
//     */
//    @AutoLog(value = "盘点-批量删除")
//    @ApiOperation(value = "盘点-批量删除", notes = "盘点-批量删除")
//    @DeleteMapping(value = "/deleteBatch")
//    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
//        this.stocktakeService.removeByIds(Arrays.asList(ids.split(",")));
//        return Result.OK("批量删除成功!");
//    }
//
//    /**
//     * 通过id查询
//     *
//     * @param id
//     * @return
//     */
//    @AutoLog(value = "盘点-通过id查询")
//    @ApiOperation(value = "盘点-通过id查询", notes = "盘点-通过id查询")
//    @GetMapping(value = "/queryById")
//    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
//        Stocktake stocktake = stocktakeService.getById(id);
//        if (stocktake == null) {
//            return Result.error("未找到对应数据");
//        }
//        return Result.OK(stocktake);
//    }
//
//    /**
//     * 导出excel
//     *
//     * @param request
//     * @param stocktake
//     */
//    @RequestMapping(value = "/exportXls")
//    public ModelAndView exportXls(HttpServletRequest request, Stocktake stocktake) {
//        return super.exportXls(request, stocktake, Stocktake.class, "盘点");
//    }
//
//    /**
//     * 通过excel导入数据
//     *
//     * @param request
//     * @param response
//     * @return
//     */
//    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
//    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
//        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
//        Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
//        for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
//            MultipartFile file = entity.getValue();// 获取上传文件对象
//            ImportParams params = new ImportParams();
//            params.setTitleRows(2);
//            params.setHeadRows(1);
//            params.setNeedSave(true);
//            try {
//                LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
//                List<Stocktake> list = ExcelImportUtil.importExcel(file.getInputStream(), Stocktake.class, params);
//                for (Stocktake stocktake : list) {
//                    if (stocktake.getArea() != null && !stocktake.getArea().isEmpty()) {
//                        if ("AGV自动仓".equals(stocktake.getArea())) {
//                            stocktake.setArea("A");
//                        }
//                        if ("高位库".equals(stocktake.getArea())) {
//                            stocktake.setArea("G");
//                        } else {
//                            stocktake.setArea("");
//                        }
//                    }
//                    ReportLabel reportLabel = reportLabelService.getOne(new LambdaQueryWrapper<ReportLabel>().eq(ReportLabel::getSkuCode, stocktake.getSkuCode()).eq(ReportLabel::getArea, stocktake.getArea()).eq(ReportLabel::getShelfCode, stocktake.getLocation()));
//                    if (reportLabel == null) {
//                        return Result.error("导入失败,未找到品番" + stocktake.getSkuCode() + ":" + stocktake.getSkuName() + "该库存");
//                    }
//                    stocktake.setStatus(0);
//                    stocktake.setNumber(reportLabel.getAvailableInventory());
//                    stocktake.setStocktakeDate(new Date());
//                    stocktake.setOperateUser(user.getRealname());
//                }
//                //update-begin-author:taoyan date:20190528 for:批量插入数据
//                long start = System.currentTimeMillis();
//                stocktakeService.saveBatch(list);
//                //400条 saveBatch消耗时间1592毫秒  循环插入消耗时间1947毫秒
//                //1200条  saveBatch消耗时间3687毫秒 循环插入消耗时间5212毫秒
//                log.info("消耗时间" + (System.currentTimeMillis() - start) + "毫秒");
//                //update-end-author:taoyan date:20190528 for:批量插入数据
//                return Result.ok("文件导入成功！数据行数：" + list.size());
//            } catch (Exception e) {
//                log.error(e.getMessage(), e);
//                return Result.error("文件导入失败:" + e.getMessage());
//            } finally {
//                try {
//                    file.getInputStream().close();
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
//            }
//        }
//        return Result.error("文件导入失败！");
//    }
//
//}
